﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RotatingWalkInMatrix
{
    public class WalkingInMatrix
    {
        public static void Main()
        {
            int matrixLength = ReadInput();
            //a test comment

            Matrix matrix = new Matrix(matrixLength);
            int number = 1;
            int row = 0;
            int col = 0;
            int horizontalStep = 1;
            int vericalStep = 1;

            UtilityMoveInMatrix.InitializeMatrixWhilePosible(matrix.MatrixArray, ref number, ref row, ref col,
                ref horizontalStep, ref vericalStep);

            UtilityMoveInMatrix.FindEmtyCell(matrix.MatrixArray, out row, out col);

            if (row != -1 && col != -1)
            {
             horizontalStep = 1;
             vericalStep = 1;

             UtilityMoveInMatrix.InitializeMatrixWhilePosible(matrix.MatrixArray, ref number, ref row,
             ref col, ref horizontalStep, ref vericalStep);
            }

            PrintMatrix(matrixLength, matrix);
        }
  
        private static int ReadInput()
        {
            Console.WriteLine("Enter a positive number ");
            string input = Console.ReadLine();
            int matrixLength = 0;

            while (!int.TryParse(input, out matrixLength) || matrixLength < 0 || matrixLength > 100)
            {
                Console.WriteLine("You haven't entered a correct positive number");
                input = Console.ReadLine();
            }
            return matrixLength;
        }
  
        private static void PrintMatrix(int matrixLength, Matrix matrix)
        {
            for (int matrixRow = 0; matrixRow < matrixLength; matrixRow++)
            {
                for (int matrixCol = 0; matrixCol < matrixLength; matrixCol++)
                {
                    Console.Write("{0,3}", matrix.MatrixArray[matrixRow, matrixCol]);
                }

                Console.WriteLine();
            }
        }
    }
}
